package com.pxmeta.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.core.redis.RedisCache;
import com.pxmeta.order.service.IOrderService;
import com.pxmeta.order.service.IUserOrderRecordService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.dto.DisableUserDTO;
import com.pxmeta.user.entity.dto.UpdateUserAccountDTO;
import com.pxmeta.user.entity.dto.UpdateUserNumberDTO;
import com.pxmeta.user.entity.dto.UserDTO;
import com.pxmeta.user.entity.vo.UserRechargeVO;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.user.service.IUserWithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.glassfish.jersey.internal.guava.Sets;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.pxmeta.common.constant.RedisKeyConstants.TEST_ACCOUNT_KEY;

@RestController
@Api(value = "用户管理",tags = "用户")
@RequestMapping(value = "/user")
@CrossOrigin
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;
    private final RedisCache redisCache;
    private final IOrderService orderService;
    private final IUserWithdrawService userWithdrawService;


    @GetMapping(value = "/getList")
    public AjaxResult<List<User>> getList(@RequestParam Integer page,
                                          @RequestParam Integer size,
                                          @RequestParam String keyword){

       IPage<User> result = userService.searchUser(page, size, keyword);
        for (User user : result.getRecords()) {
            boolean disable = StpUtil.isDisable(user.getId());
            user.setEnabled(disable?0:1);
            user.setDisabledTime(StpUtil.getDisableTime(user.getId()));
        }
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @PutMapping("/updateAccount")
    public AjaxResult updateAccount(@RequestBody UpdateUserAccountDTO dto){
        userService.updateUserAccount(dto);
        return AjaxResult.success();
    }


    @PostMapping("/disable")
    public AjaxResult disableUser(@RequestBody DisableUserDTO dto){
        long disableTime = 0L;
        //1分钟，2小时，3天，4年，5永久
        Integer time = dto.getTime();
        switch (dto.getTimeUnit()){
            case 1: //分钟
                disableTime = time * 60L;
                break;
            case 2:
                disableTime = time * 60 * 60L;
                break;
            case 3:
                disableTime = time * 60 * 60 * 24L;
                break;
            case 4:
                disableTime = time * 60 * 60 * 24 * 365L;
                break;
            case 5:
                disableTime = -1L;
                break;
        }
        Long userId = dto.getUserId();
        StpUtil.disable(userId,disableTime);
        StpUtil.logout(userId);
        return AjaxResult.success();
    }

    @PutMapping("/unDisable/{id}")
    public AjaxResult unDisable(@PathVariable Long id){
        StpUtil.untieDisable(id);
        return AjaxResult.success();
    }


    @GetMapping("/addTestAccount")
    public AjaxResult addTestAccount(@RequestParam String phone){
        HashSet<String> set = Sets.newHashSet();
        set.add(phone);
        redisCache.setCacheSet(TEST_ACCOUNT_KEY,set);
        return AjaxResult.success();
    }

    @GetMapping("/getUserDetail")
    @ApiOperation("获取用户详情")
    public AjaxResult getUserDetail(@RequestParam Long userId){
        UserDTO userInfo = userService.getUserInfo(userId, userId);
        return AjaxResult.success(userInfo);
    }


    @GetMapping("/getRechargeList")
    @ApiOperation("获取充值记录列表")
    public AjaxResult getRechargeList(@RequestParam Integer page,
                                      @RequestParam Integer size,
                                      @RequestParam(required = false) String keyword,
                                      @RequestParam(required = false) String startTime,
                                      @RequestParam(required = false) String endTime,
                                      @RequestParam(required = false) Integer status){
        IPage<UserRechargeVO> result = orderService.getRechargeList(page,size, keyword,status, startTime, endTime);
        return AjaxResult.successPage(result.getRecords(),result.getTotal());
    }


    @GetMapping("/getTotalRecharge")
    @ApiOperation("获取总充值金额")
    public AjaxResult getTotalRecharge(@RequestParam(required = false) String keyword,
                                       @RequestParam(required = false) Integer status,
                                       @RequestParam(required = false) String startTime,
                                       @RequestParam(required = false) String endTime){
        Double totalRecharge = orderService.getTotalRecharge(keyword, status, startTime, endTime);
        Double totalWithdraw = userWithdrawService.getTotalWithdraw(keyword, status, startTime, endTime);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("totalRecharge", totalRecharge);
        map.put("totalWithdraw",totalWithdraw);
        return AjaxResult.success(map);
    }



    @GetMapping("/getTestAccount")
    public AjaxResult getTestAccount(){
        Set<String> set = redisCache.getCacheSet(TEST_ACCOUNT_KEY);
        return AjaxResult.success(set);
    }


    @PutMapping("/updateBalance")
    public AjaxResult updateBalance(@RequestBody UpdateUserNumberDTO dto){
        userService.updateBalance(dto);
        return AjaxResult.success();
    }


    @PutMapping("/updateProfit")
    public AjaxResult updateProfit(@RequestBody UpdateUserNumberDTO dto){
        userService.updateProfit(dto);
        return AjaxResult.success();
    }


}
